package com.rupertjones.globalcron.server.web.service;

import com.rupertjones.globalcron.server.dao.SessionContextProvider;
import com.rupertjones.globalcron.server.dao.SystemSessionContextProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * <p>&copy Rupert Jones 2012</p>
 *
 * @author rup
 */
public class WebSessionSessionContextProvider implements SessionContextProvider {

    private SystemSessionContextProvider systemSessionContextProvider = new SystemSessionContextProvider();

    @Override
    public String getCurrentUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null) {
            return (String) authentication.getPrincipal();
        } else {
            return systemSessionContextProvider.getCurrentUser();
        }
    }
}
